﻿using UnityEngine;
using System.Collections;

public class AdaptiveUI : MonoBehaviour {

    public UIRoot uiRoot;
	void Awake(){
        AdaptiveResetUI();
		UICamera.onScreenResize += ScreenSizeChanged;
	}
	
	void OnDestroy() { 
        UICamera.onScreenResize -= ScreenSizeChanged; 
    }
	
	void ScreenSizeChanged() { AdaptiveResetUI(); }
	
	private void AdaptiveResetUI() 
	{
        //Debug.Log("AdaptiveResetUI" 
            //+ " => Screen.width "  + Screen.width 
            //+ " => Screen.height " + Screen.height);

		int ManualWidth = 1152; 
		int ManualHeight = 648;

        //UIRoot uiRoot = GameObject.FindObjectOfType<UIRoot>(); 
		if (uiRoot != null) 
		{ 
			if (System.Convert.ToSingle(Screen.height) / Screen.width > System.Convert.ToSingle(ManualHeight) / ManualWidth) 
				uiRoot.manualHeight = Mathf.RoundToInt(System.Convert.ToSingle(ManualWidth) / Screen.width * Screen.height); 
			else 
				uiRoot.manualHeight = ManualHeight; 
		}
        else
        {
            Debug.Log(transform.name +  "this >!!");
        }

	} 
}
